<?php

/**
 * @author  PVer
 * @date    2018-5-8 8:42:08
 * @version V1.0
 * @desc    
 */

namespace app\daikuan\controller;

use think\Validate;
use cmf\controller\AdminBaseController;
use app\user\model\UserModel;
use app\daikuan\model\LiscustomerModel;
use app\daikuan\model\RoleUserModel;
use app\daikuan\model\CustomerLogisticsModel;

class LiscustomerController extends AdminBaseController {

    public function index() {
        $map = array();
        $like_map = array();
        $name = input('name');
        if (!empty($name)) {
            $condition = "(a.user_nickname like '%$name%' or a.mobile like '%$name%' or a.id like '%$name%') ";
        }
        $map['user_category'] = 4;
        $dk_status = input('dk_status', -1, 'intval');
        if ($dk_status >= 0) {
            $map['a.dk_status'] = $dk_status;
        }
//        $list = model('Liscustomer')->where($map)
//                ->paginate();
        $list = db('user a ')->join(array(array('dk_liscustomer b', 'a.id=b.UserId', 'LEFT')
                ))
                ->field(' a.id,a.user_nickname,a.user_login,a.dk_status,b.* ')
                ->where($map)
                ->where($condition)
                ->paginate();
        $page = $list->render();
        $this->assign('dk_status', $dk_status);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function modifypass() {
        $id = input('id');
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function add() {

        return $this->fetch();
    }

    public function addcustsys() {

        return $this->fetch();
    }

    public function edit() {
        $id = input('id');
        $map = array();
        $map["a.id"] = $id;
        $info = db('user a ')->join(array(array('dk_liscustomer b', 'a.id=b.UserId', 'LEFT')
                ))
                ->where($map)
                ->field('a.id,a.user_nickname,b.*')
                ->find();
        if (empty($info)) {
            $this->error("数据错误");
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function audit() {
        $id = input('id');
        $map = array();
        $map["a.id"] = $id;
        $info = db('user a ')->join(array(array('dk_liscustomer b', 'a.id=b.UserId', 'LEFT')
                ))
                ->where($map)
                ->field('a.id,a.user_nickname,b.*')
                ->find();
        if (empty($info)) {
            $this->error("数据错误");
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function auditPost() {
        if ($this->request->isPost()) {
            $id = input('id');
            $data = input('post.');
            $result = false;
            $userModel = new UserModel();
            if (isset($data['audit'])) {
                $arr = array();
                $arr["dk_status"] = 1;
                $result = $userModel->save($arr, ['id' => $id]);
            } elseif (isset($data['refuse'])) {
                $arr = array();
                $arr["dk_status"] = 2;
                $result = $userModel->save($arr, ['id' => $id]);
            }
            if ($result !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        }
    }

    public function editPost() {
        if ($this->request->isPost()) {
            $id = input('id');
            $data = input('post.');
            $result = false;
            $arr = array();
            $arr1['CompanyName'] = $data['CompanyName'];
            $arr1['CompanyType'] = $data['CompanyType'];
            $arr1['Legal'] = $data['Legal'];
            $arr1['LegalPhone'] = $data['LegalPhone'];
            $arr1['RegistAmount'] = $data['RegistAmount'];
            $arr1['CreateDate'] = $data['CreateDate'];
            $arr1['WorkDays'] = $data['WorkDays'];
            $arr1['BusLis'] = $data['imgid1'];
            //$arr1['LogisticsId'] = $data['LogisticsId'];
            $arr1['cooperation'] = htmlentities($data['cooperation']);
            $result = model('Liscustomer')->save($arr1, ['Id' => $id], 'Id');

            if ($result !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        }
    }

    public function addSysPost() {
        $validate = new Validate([
            'user_login' => 'require',
            'user_pass' => 'require|min:6|max:12'
        ]);
        $validate->message([
            'user_login.require' => '用户名不能为空',
            'user_pass.require' => '密码不能为空',
            'user_pass.min' => '密码长度不能小于6',
            'user_pass.max' => '密码长度最大12',
        ]);
        $data = $this->request->post();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $userModel = new UserModel();
        $user = array();
        $user['user_login'] = $data['user_login'];
        $user['user_pass'] = cmf_password($data['user_pass']);
        $user['user_nickname'] = $data['user_nickname'];

        $user['user_category'] = 3;
        $user['user_type'] = 5;
        $user['last_login_time'] = strtotime('now');
        $user['create_time'] = strtotime('now');
        $isHave = $userModel->checkSysCode($user);
        if ($isHave == 1) {
            $this->error("用户名已存在");
            exit();
        }
        $result = $userModel->save($user);
        if ($result !== false) {
            $this->success("操作成功", url("index"));
        } else {
            $this->error("操作失败");
        }
    }

    public function addpost() {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'user_login' => 'require',
                'user_pass' => 'require|min:6|max:12',
                //'UserName' => 'require',
                'CompanyName' => 'require',
            ]);
            $validate->message([
                'user_login.require' => '用户名不能为空',
                'user_pass.require' => '密码不能为空',
                'user_pass.min' => '密码长度不能小于6',
                'user_pass.max' => '密码长度最大12',
              //  'UserName.require' => '物流公司不能为空',
                'CompanyName.require' => '公司名称不能为空'
            ]);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            //修改身份证
            $userModel = new UserModel();
            $user = array();
            $user['user_login'] = $data['user_login'];
            $user['user_pass'] = cmf_password($data['user_pass']);
            $user['user_nickname'] = $data['CompanyName'];
            $user['mobile'] = $data['LegalPhone'];
            $user['user_category'] = 4;
            $user['user_type'] = 6;
            $isHave = $userModel->checkCompanyCode($user);
            if ($isHave == 1) {
                $this->error("用户名已存在");
                exit();
            }
            if ($isHave == 2) {
                $this->error("该公司已经存在");
                exit();
            }
            $result_1 = $userModel->save($user);
            if ($result_1 > 0) {
                $roleUser = new RoleUserModel();
                $role = array();
                $role['role_id'] = 6;
                $role['user_id'] = $userModel->id;
                $roleUser->save($role);
                $customerLogisticsModel = new CustomerLogisticsModel();
                $clt = array();
                $clt["customerid"] = $userModel->id;
                $clt["logid"] = $data['UserId'];
                $clt_result = $customerLogisticsModel->save($clt);
                $liscustomerModel = new LiscustomerModel();
                $arr1 = array();
                $arr1['UserId'] = $userModel->id;
                $arr1['CompanyName'] = $data['CompanyName'];
                $arr1['CompanyType'] = $data['CompanyType'];
                $arr1['Legal'] = $data['Legal'];
                $arr1['LegalPhone'] = $data['LegalPhone'];
                $arr1['RegistAmount'] = $data['RegistAmount'];
                $arr1['CreateDate'] = $data['CreateDate'];
                $arr1['WorkDays'] = $data['WorkDays'];
                $arr1['BusLis'] = $data['imgid1'];
                //$arr1['LogisticsId'] = $data['UserId'];
                $arr1['cooperation'] = htmlentities($data['cooperation']);
                $res1 = $liscustomerModel->where('UserId=' . $userModel->id)->select();
                if (count($res1) > 0) {
                    $result_2 = $liscustomerModel->save($arr1, 'UserId=' . $userModel->id);
                } else {
                    $result_2 = $liscustomerModel->save($arr1);
                }
                if ($result_1 !== false && $result_2 !== false && $clt_result !== false) {
                    $this->success("操作成功", url("index"));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error("操作失败");
            }
        } else {
            $this->error("请求错误");
            return $this->fetch(':index');
        }
    }

    public function getUserByName() {
        $data = input('post.');
        $map = array();
        $map['user_category'] = 2;
        $name = $data["text"];
        if (!empty($name)) {
            $map['user_nickname'] = array('like', "%$name%");
        }
        $userModel = new UserModel();
        $result = $userModel
                ->where($map)
                ->select();
//$result = model('Crop')->field('cropname')->select();
        $arr = array();
        foreach ($result as $row) {
            $arr[] = $row['user_nickname'];
        }

        return $result;
    }

    public function modifypassPost() {

        if ($this->request->isPost()) {
            $data = $this->request->post();

            $userModel = new UserModel();
            $user = array();
            $user['user_pass'] = cmf_password($data['user_pass']);
            $result = $userModel->save($user, 'id=' . $data['id']);
            if ($result !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        } else {
            $this->error("数据错误");
            return $this->fetch('index');
        }
    }

}
